/**
 * 获取 Chrome 存储
 * @date 2023-12-21
 * @param {any} key
 * @returns {any}
 */
export async function getChromeStorage(key) {
  const result = await chrome.storage.local.get([key]);
  return result[key];
}
/**
 * 设置 Chrome 存储
 * @date 2023-12-21
 * @param {any} key
 * @param {any} value
 * @returns {any}
 */
export async function setChromeStorage(key, value) {
  return await chrome.storage.local.set({ [key]: value });
}

/**
 * 给background.js发送消息
 * @date 2023-12-21
 * @param {any} event
 * @param {any} responseCallBack
 * @returns {any}
 */
export const sendMsgToBackground = function (event, responseCallBack) {
  console.log("通信", event);
  chrome.runtime.sendMessage(event, responseCallBack);
};

export async function doAction(action, data) {
  if (!action) {
    const result = { code: -1, _success: false, _msg: "action is null" };
    console.error(result);
    return result;
  }
  try {
    return await action(data);
  } catch (e) {
    console.error(e);
    return {
      code: -1,
      _success: false,
      _msg: (e && (e.message || e._msg)) || e
    };
  }
}
